module clkDiv#(
        parameter CLK_MHZ = 50,        // 主时钟频率（单位：MHz）
        parameter SCAN_MS = 1, // 扫描时钟间隔1ms
        parameter DEBOUNCE_MS = 20 // 按键消抖20ms
)(
    input logic sys_clk,
    output logic clk_scan,
    output logic clk_debounce
);

// 计算所需时钟周期数和计数器位宽

//1.扫描时间间隔
localparam SCAN_MAX = (SCAN_MS * CLK_MHZ * 1000)/2;
localparam SCAN_WIDTH = $clog2(SCAN_MAX) + 1; 

//2.按键消抖间隔
localparam DEBOUNCE_MAX = (DEBOUNCE_MS * CLK_MHZ * 1000)/2;
localparam DEBOUNCE_WIDTH = $clog2(DEBOUNCE_MAX) + 1; 

//计数器
logic [SCAN_WIDTH-1:0] counter_scan;
logic [DEBOUNCE_WIDTH-1:0] counter_debounce;


always_ff @(posedge sys_clk) begin
    //扫描时钟分频
    if(counter_scan == SCAN_MAX-1)begin //计数达到阈值
        clk_scan<=~clk_scan; //翻转时钟信号
        counter_scan<=0; //计数器清零
    end else begin
        counter_scan<=counter_scan+1;
    end

    //按键消抖分频
    if(counter_debounce == DEBOUNCE_MAX-1)begin //计数达到阈值
        clk_debounce<=~clk_debounce; //翻转时钟信号
        counter_debounce<=0; //计数器清零
    end else begin
        counter_debounce<=counter_debounce+1;
    end

end
endmodule